//#include "Utility.h"
#include "DB.h"
#include <Windows.h>
#include <stdio.h>
extern int Create(const char* tableName,col* colAddr,int count);
extern int insertNormal(const char* tableName,colValue* valueArr);
void convert(char* input,char* output) {
	int begin,end;
	int i;
	for(i = 0 ;i < strlen(input); i++) {
		if(input[i] == '"') {
			begin = i;
			break;
		}
	}
	for(i = strlen(input) - 1; i>= 0; i--) {
		if(input[i] =='"') {
			end = i;
			break;
		}
	}
	strncpy(output,input+begin + 1, end - begin - 1);
}
void order(struct col*, int*);
int main(int argc, char** argv) {
        if (argc != 2)
            return 0;
	DBInit();
	HANDLE hThread = BufCheck();
	char* tableName = argv[1];
        char txtFile[256];
        sprintf(txtFile, "%s.txt", argv[1]);
	FILE* file = fopen(txtFile, "r");
	char s[256];
	char* state;
	col_num = 0;
	int i;
	int row_num = 0;
	struct col col_list_t[256];
	state = fgets(s,256,file);							//resovle first line
	if(state == NULL) {
		printf("empty file?\n");
		return;
	}
	state = strtok(s,",");
	while(state != NULL) {
		char temp[256];
		convert(state,temp);
		strcpy(col_list_t[col_num].name, temp);
		memset(temp,0,256);
		col_num++;
		state = strtok(NULL,",");
	}
	col_list = (struct col*)malloc(col_num * sizeof(struct col));
	value_list = (struct colValue*)malloc(col_num * sizeof(struct colValue));
	memcpy(col_list,col_list_t,sizeof(struct col) * col_num);
	for(i = 0; i < col_num; i++) printf("%s\n",col_list[i].name);
	printf("total column is: %d\n",col_num);

	/*
	 *now we have got all the column in the original data file
	*/

	/*
	 * below we read the already defined db file
	 */
	        char dbfFile[256];
                sprintf(dbfFile, "%s.dbf", argv[1]);
                FILE* db_file = fopen(dbfFile, "rb");
		unsigned long recordCount = 1000;
		int colCount = 0;
		struct col* db_col_list;
		int* map_list;
		fread(&recordCount, sizeof(unsigned long), 1 , db_file);
		fread(&colCount, sizeof(int), 1, db_file);
		printf("%d %d\n",colCount,sizeof(col));
		if(colCount != col_num) {
			printf("Col number not equal!!!\n");
			return;
		}
		map_list = (int*)malloc(colCount * sizeof(int));
		db_col_list = (struct col*)malloc(colCount * sizeof(struct col));
		memset(db_col_list,0, sizeof(col) * colCount);
		for(i = 0; i < colCount; i++) {
			fread(&db_col_list[i], sizeof(col),1,db_file);
		}
		for(i = 0; i < colCount; i++) {
			printf("%s %d %d\n",db_col_list[i].name, db_col_list[i].size ,db_col_list[i].type);
		}
		order(db_col_list,map_list);
		for(i = 0; i < col_num; i++) printf("%d ",map_list[i]);
		fclose(db_file);
	/*
	 * config end
	 */

	/*
	 * below we test each statement of input file
	 */
	while((state = fgets(s,256,file)) != NULL) {
		state = strtok(s,",");
		char temp[256];
		i = 0;
		while(state != NULL) {
			memset(temp,0,256);
			convert(state,temp);
			if(db_col_list[map_list[i]].type == DATA_INT) {					//if int
				value_list[map_list[i]].isChar = 1;
				value_list[map_list[i]].value.intValue = atoi(temp);
			//	printf("%s %d\n",state,atoi(temp));
			}
			else if(db_col_list[map_list[i]].type == DATA_CHAR) {
				value_list[map_list[i]].isChar = 0;
				strcpy(value_list[map_list[i]].value.charValue,temp);
			}
			else if(db_col_list[map_list[i]].type == DATA_VCHAR) {
				value_list[map_list[i]].isChar = 0;
				strcpy(value_list[map_list[i]].value.charValue,temp);
			}
			else {
				printf("Col type undefine!!!\n");
				return;
			}
			i++;
			state = strtok(NULL,",");
		}


		/*
		 * One Column has got, do insert
		 * 
		 */
		
		insertNormal(tableName,value_list);	
		printf("%dth row: ",row_num);
		for(i = 0; i < col_num; i++) {
			if(db_col_list[map_list[i]].type == DATA_INT) printf("%d ",value_list[map_list[i]].value.intValue);
			else	printf("%s ", value_list[map_list[i]].value.charValue);
		}
		printf("\n");
		row_num++;
	}

	printf("Total row is: %d\n",row_num);
	free(map_list);

	DBCloseFile(DBFileIsOpen(tableName));
}

void order(struct col* list, int* map_list) {
	int i,j;
	for(i = 0; i < col_num; i++) {
		for(j = 0 ; j < col_num; j++) {
			if(strcmp(list[j].name,col_list[i].name) == 0) {
				map_list[i] = j;
				break;
			}
		}
	}
}
